from django.http import HttpResponse
from django.conf import settings
import json
import logging
import datetime
import os
from xml.etree import ElementTree as ET
from xml.dom.minidom import Document
import time
from concurrent.futures import ThreadPoolExecutor,as_completed,ProcessPoolExecutor
import collections
import sys 
import sqlite3 

oozie_home='/export/datamarket/oozie/oozie-4.3.1/bin/'
hadoop_home='/export/datamarket/hadoop/bin/'
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',datefmt='%a, %d %b %Y %H:%M:%S')
def upload(request):
	if request.method == 'POST':
	

		obj = request.FILES.get('oozieShell')
		import os
		f = os.path.join(settings.OOZIE_SHELL_ROOT,'upload',obj.name)
		fileName=f.split("/")[-1]    
		logging.info("\n\n\n\t要上传的脚本为："+fileName+"\n\n")
		#print(os.path.join(settings.OOZIE_SHELL_ROOT,obj.name))
		#不上传重复文件
		if not os.path.exists(f):
			with open(f, 'wb') as file:
				for chunk in obj.chunks():
					file.write(chunk)
			oozieShell=fileName
			save_oozie_shell(oozieShell)
			db_path = os.path.normpath('/export/datamarket/OozieProperties/oozie_project/db.sqlite3')  
			db = sqlite3.connect(db_path) 
			# 通过cursor创建游标
			cursor = db.cursor()
			sql_cmd=  "select * from oozie_shell where oozieShell='{}'".format(oozieShell) 
			cursor.execute(sql_cmd)
			shellID=cursor.fetchone()
			shellID=shellID[0]
			cursor.close()
			db.commit()
			db.close()
			result={"result":"上传成功","oozieShell":obj.name,"oozie_shell_root":os.path.join(settings.OOZIE_SHELL_ROOT,"upload"),"shellID":shellID,"code":0}
			logging.info(result)	
			logging.info("\n\n\n\t"+fileName+"脚本上传成功！\n\n")

			return HttpResponse(json.dumps(result,ensure_ascii=False), content_type="application/json")
		else:
			result={"result":fileName+"脚本已存在！","code":1}
			logging.info("\n\n\n\t"+fileName+"脚本已经存在！\n\n")
			return HttpResponse(json.dumps(result,ensure_ascii=False), content_type="application/json")
	else:
		pass
	
def save_oozie_shell(oozieShell):

	oozieShell=oozieShell

	db_path = os.path.normpath('/export/datamarket/OozieProperties/oozie_project/db.sqlite3')  
	db = sqlite3.connect(db_path) 
	
	# 通过cursor创建游标
	cursor = db.cursor()
	sql_cmd = '''select * from oozie_shell where oozieShell = '%s' ''' % oozieShell
	cursor.execute(sql_cmd)
	res = cursor.fetchall()
	suc = True
	if len(res) > 0 :
		logging.info('%s 脚本已存在，无需插入' % oozieShell)
		suc = True
	else:
		sql_cmd_2 =  "insert into oozie_shell(shellID,oozieShell) values(null,'{}')".format(oozieShell) 
		cursor.execute(sql_cmd_2)
		logging.info('%s added 成功！' % oozieShell)
		suc = False
	cursor.close()
	db.commit()
	db.close()
	return suc
